package com.xjiuge.fruit.controller.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xjiuge.fruit.bean.Menu;
import com.xjiuge.fruit.bean.User;
import com.xjiuge.fruit.common.JsonResult;
import com.xjiuge.fruit.common.Response;
import com.xjiuge.fruit.common.ResponseCodeConstant;
import com.xjiuge.fruit.common.ValidateUtil;
import com.xjiuge.fruit.service.MenuService;

@Controller("MenuController")
@RequestMapping("/menu")
public class MenuController {

	public final static Logger LOG = Logger.getLogger(MenuController.class);
	
	@Resource(name = "menuService")
	private MenuService menuService;
	
	/**
	 * 获取菜单树
	 * MenuController.tree<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月3日  上午11:27:30</p>
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public JsonResult tree(){
		Integer code = null;
		String msg = "";
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> menuList = menuService.getTreeList();
			
			data.put("menuList", menuList);
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "获取菜单树异常"+e;
			LOG.error("获取菜单树异常"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		response.setData(data);
		return new JsonResult(response);
	}
	
	/**
	 * 添加菜单
	 * MenuController.save<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月2日  下午3:56:22</p>
	 * @param menu
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult save(Menu menu,HttpSession httpSession){
		Integer code = null;
		String msg = "";
		Response response = new Response();
		User loginUser = (User) httpSession.getAttribute("user");
		if(ValidateUtil.isNull(menu.getMenuName())){
			code = ResponseCodeConstant.Common.Business_exception.getCode();
			msg = "菜单名称不能为空";
			response.setCode(code);
			response.setMsg(msg);
			return new JsonResult(response);
		}
		if(ValidateUtil.isNull(menu.getParentId())){
			code = ResponseCodeConstant.Common.Business_exception.getCode();
			msg = "父级菜单ID不能为空";
			response.setCode(code);
			response.setMsg(msg);
			return new JsonResult(response);
		}
		try {
			menuService.save(menu,loginUser);
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "添加菜单异常"+e;
			LOG.error("添加菜单异常"+e);
		}
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
	
	/**
	 * 通过父ID查询子节点集合
	 * MenuController.selectMenuListByPid<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月3日  下午1:53:08</p>
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/selectMenuListByPid",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult selectMenuListByPid(Menu menu){
		Integer code = null;
		String msg = "";
		Response response = new Response();
		Map<String, Object> data = new HashMap<String,Object>();
		if(ValidateUtil.isNull(menu.getParentId())){
			code = ResponseCodeConstant.Common.Business_exception.getCode();
			msg = "父级ID不能为空";
			response.setCode(code);
			response.setMsg(msg);
			return new JsonResult(response);
		}
		try {
			List<Menu> menuList = menuService.selectMenuListByPid(menu.getParentId());
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
			data.put("menuList", menuList);
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "根据父ID查询子节点列表异常"+e;
			LOG.error("根据父ID查询子节点列表异常"+e);
		}
		response.setCode(code);
		response.setMsg(msg);
		response.setData(data);
		return new JsonResult(response);
	}
	
	/**
	 * 批量删除菜单
	 * MenuController.deleteMenu<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月3日  下午4:57:37</p>
	 * @return
	 */
	@RequestMapping(value = "/deleteMenu",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult deleteMenu(String [] ids){
		Integer code = null;
		String msg = "";
		try {
			if(ids.length>0){
				menuService.deleteByIds(ids);
				code = ResponseCodeConstant.Common.success.getCode();
				msg = ResponseCodeConstant.Common.success.getValue();
			}else{
				code = ResponseCodeConstant.Common.Business_exception.getCode();
				msg = "菜单ID不能为空！";
			}
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "删除菜单异常"+e;
			LOG.error("删除菜单异常"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
	
	/**
	 * 编辑菜单
	 * MenuController.update<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月6日  上午11:06:31</p>
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(Menu menu,HttpSession httpSession){
		Integer code = null;
		String msg = "";
		Response response = new Response();
		User user = (User) httpSession.getAttribute("user");
		if(ValidateUtil.isNull(menu.getMenuId())){
			code = ResponseCodeConstant.Common.Business_exception.getCode();
			msg = "菜单ID不能为空";
			response.setCode(code);
			response.setMsg(msg);
			return new JsonResult(response);
		}
		try {
			menuService.update(menu,user);
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "编辑菜单异常"+e;
			LOG.error("编辑菜单异常"+e);
		}
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
	
	/**
	 * 获取角色权限
	 * MenuController.auth<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月6日  下午3:01:25</p>
	 * @return
	 */
	@RequestMapping(value = "/auth",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult auth(){
		Integer code = null;
		String msg = "";
		Map<String, Object> data = new HashMap<>();
		try {
			List<Map<String, Object>> menuList = menuService.auth();
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
			data.put("menuList", menuList);
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "编辑菜单异常"+e;
			LOG.error("编辑菜单异常"+e);
		}
		Response response = new Response();
		response.setCode(code);
		response.setMsg(msg);
		response.setData(data);
		return new JsonResult(response);
	}
}
